package U2;

import java.util.List;
import java.io.IOException;
import java.util.Map;
public class Main {
    public static void main(String[] args) throws IOException {
        Main main = new Main();
        main.startup(
                "D:\\Java\\work\\Students.txt",
                "http://139.186.26.196/javaweb/data/math.txt",
                "http://139.186.26.196/javaweb/data/chinese.txt",
                "http://139.186.26.196/javaweb/data/english.txt"
        );
        main.execute();
    }

    public void startup(String uriStudent, String uriMath, String uriChinese, String uriEnglish) throws IOException {
       List<Student> students = this.loadStudents(uriStudent);
        // 加载学生基本信息
        StudentManager manager = StudentManager.getInstance();
        students.forEach(s -> {
            manager.addStudent(s);
        });
        // 加载数学成绩
        Map<String, Integer> mathScores = ScoreReaderFactory.create(uriMath).read();
        mathScores.forEach((Num, score) -> {
            Student student = manager.getStudentByNum(Num);
            student.setMath(score);
        });
        // 加载语文成绩
        Map<String, Integer> chineseScores = ScoreReaderFactory.create(uriChinese).read();
        chineseScores.forEach((Num, score) -> {
            manager.getStudentByNum(Num).setChinese(score);
        });

        // 加载外语成绩
        Map<String, Integer> englishScores = ScoreReaderFactory.create(uriEnglish).read();
        englishScores.forEach((Num, score) -> {
            manager.getStudentByNum(Num).setEnglish(score);
        });
    }
   public void execute() throws IOException {
        StudentManager manager = StudentManager.getInstance();
        System.out.println("学生成绩信息：");
        manager.printAll();
        System.out.println("\n");
        manager.saveAs("D:\\Java\\work\\Student.txt");
        int i=1;
        List<Student> students = manager.sortByTotal();
        System.out.println("总分排名：");
        for (Student student : students) {
            System.out.println(String.format("%3d %s %-4s %4s", i++,student.getNum(), student.getName(), student.getTotal()));
        }
        System.out.println("\n");

        //查询数学课程的某分数段学生
        System.out.println("数学成绩在80-85的学生： ");
        List <Student> result = manager.findByScore(ClassType.MATH, 80, 85);
        for (Student student : result) {
            System.out.println(String.format("%s %s %s", student.getNum(), student.getName(), student.getMath()));
        }
        System.out.println("\n");

        //查找某平均分区间的学生
        System.out.println("平均分在90-100的学生： ");
        List <Student> list = manager.findByAverage( 90, 100);
        for (Student student : list) {
            System.out.println(String.format("%s %-4s %.2f", student.getNum(), student.getName(), student.getAverage()));
        }
        System.out.println("\n");

        //查找学生信息
        String Num = "222021321072053";
        Student student = manager.findByNum(Num);
        System.out.println(student.toString());
        System.out.println("\n");

        System.out.println("查找李姓学生的成绩信息： ");
        manager.visit(new IStudentVisitor() {
            @Override
            public void visit(Student student) {
                if (student.getName().startsWith("李")) {
                   System.out.println(student);
                }
            }
        });
    }
    public List<Student> loadStudents(String uri) throws IOException {
        IStudentReader reader = StudentReaderFactory.create(uri);
        return reader.read();
    }
}